import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '@/views/HomeView.vue'
import LoginView from '@/views/LoginView.vue'
import CustomerView from '@/views/CustomerView.vue'
import BusinessView from '@/views/BusinessView.vue'
import LayoutView from '@/views/Layout.vue'
import WorkOrderView from '@/views/WorkOrderView.vue'
import UserView from '@/views/UserView.vue'


Vue.use(VueRouter);

const routes = [
  {
    path: '/login',
    name: 'login',
    component: LoginView
  },
  {
    path: '/',
    name: 'Layout',
    component: LayoutView,
    children: [
      {
        path: '',
        name: 'home',
        component: HomeView
      },
      {
        path: 'customer',
        name: 'customer',
        component: CustomerView
      },
      {
        path: 'business',
        name: 'business',
        component: BusinessView
      },
      {
        path: 'workOrder',
        name: 'workOrder',
        component: WorkOrderView
      },
      {
        path: 'user',
        name: 'user',
        component: UserView
      },
    ]
  },
  
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 定义路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next();
  }
  const user = localStorage.getItem('user');
  if (!user && to.path !== '/login') {
    next('/login');
  } else {
    next();
  }
})

export default router
